package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.*;
import com.itheima.service.SetMealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class SetMealServiceImpl implements SetMealService {
    //获取dao层代理对象
    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    //获取jedis对象
    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    @Override
    public Result findSetMealByPage(QueryPageBean queryPageBean) {
        //创建map集合,存储list和total
        HashMap<String, Object> map = new HashMap<>();
        //开启分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Setmeal> list = setMealMapper.findSetMealByPage(queryPageBean.getQueryString());
        if (list == null) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }else{

            /*//遍历集合
            for (Setmeal setmeal : list) {
                String imgName = setmeal.getImg();
                setmeal.setImg("http://qd1iy19wc.bkt.clouddn.com/"+imgName);
            }*/

            //创建pageInfo对象
            PageInfo<Setmeal> pageInfo = new PageInfo<>(list);
            //存储
            map.put("list",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        }
    }

    /**
     *添加套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @Override
    public Result addSetMeal(int[] checkgroupIds, Setmeal setmeal) {
        if(checkgroupIds == null || checkgroupIds.length == 0 || setmeal.getImg() == null){
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }else{
            //回显主键
            //添加检查组
            int row = setMealMapper.addSetMeal(setmeal);

            //将要保存到数据库中的图片保存到redis中
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            resource.close();
            //判断检查组是否添加成功
            if (row == 0){
                return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
            }else{
                //System.out.println("套餐的id是:"+setmeal.getId());
                //添加成功,接着添加检查组与检查项的关联
                boolean flag = addSetMealAndCheckGroup(checkgroupIds,setmeal.getId());
                //判断
                if (!flag){
                    return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
                }else{
                    return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
                }
            }
        }

    }

    private boolean addSetMealAndCheckGroup(int[] checkgroupIds, Integer id) {
        int num = 0;
        for (int i = 0; i < checkgroupIds.length; i++) {
            setMealMapper.addSetMealAndCheckGroup(id,checkgroupIds[i]);
            num++;
        }
        //判断
        if (num != checkgroupIds.length ){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 根据id查询套餐
     * @param strId
     * @return
     */
    @Override
    public Result findSetMealById(String strId) {
        int id = 0;
        if (strId != null) {
            id  = Integer.parseInt(strId);
        }
        //创建map集合存储检查组的信息和相关的检查项的id
        Setmeal setmeal = setMealMapper.findSetMealById(id);
        //System.out.println("查询检查组的信息为:"+setmeal);
        if (setmeal == null) {
            System.out.println("查询失败");
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }else{
            System.out.println("查询成功");
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }
    }

    /**
     * 根据套餐id查询关联的检查组
     * @param strId
     * @return
     */
    @Override
    public Result findCheckGroupIdsBySetMealId(String strId) {
        int id = 0;
        if (strId != null) {
            id  = Integer.parseInt(strId);
        }
       // System.out.println("检查组的id是:"+id);
        List<Integer> checkgroupIds = setMealMapper.findCheckGroupIdsBySetMealId(id);
        //System.out.println("查询检查组的检查项有:"+checkgroupIds);
        if (checkgroupIds == null) {//|| checkItems.size() == 0
            System.out.println("查询检查项失败");
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }else{
            System.out.println("查询检查项成功");
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkgroupIds);
        }
    }

    /**
     * 编辑套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @Override
    public Result updateSetMealById(int[] checkgroupIds, Setmeal setmeal) {
        //System.out.println("需要修改的检查组的id是"+setmeal.getId());
        if (checkgroupIds == null || checkgroupIds.length == 0 || setmeal.getImg() == null){
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }else{
            //删除检查组与检查项的关联
            setMealMapper.deleteCheckGroupIdsBySetMealId(setmeal.getId());

            //修改套餐信息
            int row = setMealMapper.updateSetMealById(setmeal);

            //将要保存到数据库中的图片保存到redis中
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            resource.close();
            if (row == 0 ){
                return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
            }else{
                //解除关联
                boolean flag = addSetMealAndCheckGroup(checkgroupIds,setmeal.getId());
                //判断
                if (!flag){
                    return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
                }else{
                    return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
                }
            }
        }
    }

    /**
     * 删除套餐
     * @param strId
     * @return
     */
    @Override
    public Result deleteSetMealById(String strId) {
        int id = 0;
        if (strId != null) {
            id  = Integer.parseInt(strId);
        }
        //先解除关系
        int row = setMealMapper.deleteCheckGroupIdsBySetMealId(id);
        if (row == 0){
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }else{
            //再删除套餐
            int i = setMealMapper.deleteSetMealById(id);
            if (i==0){
                return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
            }else{
                return new Result(false,MessageConstant.DELETE_SETMEAL_SUCCESS);
            }
        }
    }

    @Override
    public Result findAllSetMeal() {
        List<Setmeal> list = setMealMapper.findAllSetMeal();
        if (list == null) {
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }else{
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,list);
        }
    }

    @Override
    public Result findSetMealDetail(String strId) {
        int id = 0;
        if (strId != null) {
            id  = Integer.parseInt(strId);
        }

        //System.out.println("------------------------传递的套餐id是:"+id);

        Setmeal setmeal = setMealMapper.findSetMealById(id);//查询套餐的基本信息

        List<Integer> checkGroupIds = setMealMapper.findCheckGroupIdsBySetMealId(id);//根据套餐id查询相关联的检查组

        //System.out.println("------------------------图片的名字为"+setmeal.getImg());
        //System.out.println("------------------------checkGroupIds的大小为:"+checkGroupIds.size());

        if (checkGroupIds.size() == 0 || checkGroupIds == null || setmeal == null){
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }else{

            //创建一个集合存储套餐里面的关联的检查组的信息
            ArrayList<CheckGroup> checkGroupsList = new ArrayList<>();

            //遍历检查组
            for (Integer checkGroupId : checkGroupIds) {

                //根据检查组的id查询相关联的检查项
                List<Integer> checkItemIds = checkGroupMapper.findCheckItemIdsByGroupId(checkGroupId);

                //System.out.println("------------------------checkItemIds的大小为:"+checkItemIds.size());

                if (checkItemIds == null || checkItemIds.size() == 0){
                    return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
                }else {

                    //根据id查询检查组
                    CheckGroup checkGroup = checkGroupMapper.findCheckGroupById(checkGroupId);

                    //创建一个集合存储检查组里面的相关检查项
                    ArrayList<CheckItem> checkItemsList = new ArrayList<>();
                    //遍历检查项
                    for (Integer checkItemId : checkItemIds) {
                        //根据id查询检查项
                        CheckItem checkItem = checkItemMapper.findCheckItemById(checkItemId);
                        checkItemsList.add(checkItem);
                    }

                    //将集合赋值给检查组
                    checkGroup.setCheckItems(checkItemsList);

                    //System.out.println("------------------------检查组的信息为:"+checkGroup);
                    //System.out.println("------------------------检查组里的检查项的信息为:"+checkGroup.getCheckItems());

                    //将检查组的信息存到checkGroupsList中
                    checkGroupsList.add(checkGroup);
                }

            }
            //将checkGroupsList赋值给套餐
            setmeal.setCheckGroups(checkGroupsList);

            //System.out.println("------------------------套餐的信息为:"+setmeal);
            //System.out.println("------------------------套餐里面检查组的信息为:"+setmeal.getCheckGroups());

            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }
    }
}
